package com.company.project.config.security;

import com.company.project.config.jwt.JwtUserFactory;
import com.company.project.dao.RelUserRoleMapper;
import com.company.project.dao.RoleMapper;
import com.company.project.dao.UserMapper;
import com.company.project.model.RelUserRole;
import com.company.project.model.Role;
import com.company.project.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cjj
 * @Title: UserService
 * @Description: TODO
 * @date 18-7-31下午4:52
 */
@Component
@Transactional
public class UserService implements UserDetailsService {

    //用户仓储
    @Autowired
    private UserMapper userMapper;
    //用户-角色仓储
    @Autowired
    private RelUserRoleMapper relUserRoleMapper;
    //角色仓储
    @Autowired
    private RoleMapper roleMapper;

    /**
     * Locates the user based on the username. In the actual implementation, the search
     * may possibly be case sensitive, or case insensitive depending on how the
     * implementation instance is configured. In this case, the <code>UserDetails</code>
     * object that comes back may have a username that is of a different case than what
     * was actually requested..
     *
     * @param account the username identifying the user whose data is required.
     * @return a fully populated user record (never <code>null</code>)
     * @throws UsernameNotFoundException if the user could not be found or the user has no
     *                                   GrantedAuthority
     */
    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        User user = new User();
        user.setAccount(account);
        User iUser = userMapper.selectOne(user);
        if (iUser == null) {
            throw new UsernameNotFoundException("用户名不存在");
        } else {
            Condition condition = new Condition(RelUserRole.class);
            condition.createCriteria().andCondition("user_id = '"+iUser.getId()+"'");
            List<RelUserRole> userRoles = relUserRoleMapper.selectByCondition(condition);

            List<Role> role
                    = userRoles.stream().filter(x -> {
                if (roleMapper.selectByPrimaryKey(x.getRoleId()) == null) {
                    return false;
                } else return true;
            }).map(s -> roleMapper.selectByPrimaryKey(s.getRoleId())).collect(Collectors.toList());
            //实体转dto
            List<String> roleString = new ArrayList<>();
            for (int i = 0; i < role.size(); i++) {
                roleString.add(roleMapper.selectByPrimaryKey(role.get(i).getId()).getName());
            }
            iUser.setRoles(roleString);
            return JwtUserFactory.create(iUser);
        }
    }
}
